<template>
   <div class="sub-filter" v-if="filterData&&!filterLoading">
     <div class="item">
       <div class="head">品牌：</div>
       <div class="body">
         <a :class="{active:item.id===filterData.selectedBrand}" href="javascript:;" v-for="item in filterData.brands" :key="item.id" @click="changeBrand(item.id)">{{item.name}}</a>
       </div>
     </div>

     <div class="item" v-for="item in filterData.saleProperties" :key="item.id">
       <div class="head">{{item.name}}：</div>
       <div class="body">
         <a :class="{active:prop.id===item.selectedProp}" href="javascript:;" v-for="prop in item.properties" :key="prop.id" @click="changeProp(item,prop.id)">{{prop.name}}</a>
       </div>
     </div>
   </div>
<!-- 加载效果 -->
     <div v-else class="sub-filter">
    <XtxSkeleton class="item" width="800px" height="40px"  />
    <XtxSkeleton class="item" width="800px" height="40px"  />
    <XtxSkeleton class="item" width="600px" height="40px"  />
    <XtxSkeleton class="item" width="600px" height="40px"  />
    <XtxSkeleton class="item" width="600px" height="40px"  />
  </div>
</template>

<script>
import { watch, ref } from 'vue'
import { useRoute } from 'vue-router'
import { findSubCategoryFilter } from '@/api/category'
export default {
  name: 'SubFilter',
  setup (props, { emit }) {
    const route = useRoute()
    const filterData = ref({})
    const filterLoading = ref(false)// 是否处于加载状态
    watch(() => route.params.id, (newVal) => {
      if (newVal && `/category/sub/${newVal}` === route.path) {
        filterLoading.value = true
        findSubCategoryFilter(route.params.id).then(data => {
          // 给品牌加当前选中和全部
          data.result.selectedBrand = null
          data.result.brands.unshift({ id: null, name: '全部' })
          // 给属性加当前选中和全部
          data.result.saleProperties.forEach(item => {
            item.selectedProp = null
            item.properties.unshift({ id: null, name: '全部' })
          })
          filterData.value = data.result
          filterLoading.value = false
        })
      }
    }, { immediate: true })
    // 点击品牌
    function changeBrand (brandId) {
      if (filterData.value.selectedBrand === brandId) return
      filterData.value.selectedBrand = brandId
      emit('filter-change', getFilterParams())
    }
    // 点击属性
    function changeProp (item, propId) {
      if (item.selectedProp === propId) return
      item.selectedProp = propId
      emit('filter-change', getFilterParams())
    }
    // 生成请求数据
    function getFilterParams () {
      // 目标{brandId:'',attrs:[{groupName:'',propertyName:''},...]}
      const obj = { brandId: null, attrs: [] }
      // 品牌数据 id
      obj.brandId = filterData.value.selectedBrand
      // 属性数据 注意不是id而是name
      filterData.value.saleProperties.forEach(item => { // item 每行
        if (item.selectedProp) {
          const prop = item.properties.find(prop => prop.id === item.selectedProp) // prop每个
          obj.attrs.push({ groupName: item.name, propertyName: prop.name })
        }
      })
      if (obj.attrs.length === 0)obj.attrs = null // null不会发请求，而[]会
      return obj
    }

    return { filterData, filterLoading, changeBrand, changeProp }
  }
}
</script>

<style scoped lang='less'>
  // 筛选区
  .sub-filter {
    background: #fff;
    padding: 25px;
    .item {
      display: flex;
      line-height: 40px;
      .head {
        width: 80px;
        color: #999;
      }
      .body {
        flex: 1;
        a {
          margin-right: 36px;
          transition: all .3s;
          display: inline-block;
          &.active,
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
    .xtx-skeleton {
      padding: 10px 0;
    }
  }
</style>
